package com.jy.admin.rest;

import com.jsoniter.output.JsonStream;
import com.jy.admin.db.dao.CeAgentAccountDao;
import com.jy.admin.jobs.BaseJobHandler;
import com.jy.admin.services.cashPocket.ChenXingService;
import com.jy.admin.services.cashPocket.LittleOrangeService;
import com.jy.admin.annotations.AdminSignIgnore;
import com.jy.admin.auth.anno.VisitorAccessible;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: binary (mr.vencnet@gmail.com)
 * Date: 2020-06-02
 * Time: 17:18
 */
@RestController
public class OkController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OkController.class);

    @Autowired
    private BaseJobHandler baseJobHandler;

    @Autowired
    private ChenXingService chenXingService;
    @Autowired
    private CeAgentAccountDao ceAgentAccountDao;

    @AdminSignIgnore
    @RequestMapping("/ok")
    @VisitorAccessible
    public String ok() {
        return "ok22";
    }

    @AdminSignIgnore
    @RequestMapping("/testJt")
    @VisitorAccessible
    public String testJT() {

        String merchantCode = "sivanwallet";
        String url = "https://console.sivanwallet.com/hs/admin/auth/login";
        String params = "{\n" + "    \"phoneNo\": \"B003\",\n" + "    \"code\": \"23656\"\n" + "}";
        String fileName = "/home/java/open-collection/cks/" + merchantCode + "_" + "B003.txt";

        String[] command = {"curl", "-c", fileName, "--location", "--request", "POST", url, "--header", "Content-Type: application/json", "--data-raw", params};
        String loginRes = LittleOrangeService.execCurl(command);

        LOGGER.info("== testJT  ==,loginRes:{}", loginRes);


        try {
            Path path = Paths.get(fileName);
            byte[] data = Files.readAllBytes(path);
            String result = new String(data, "utf-8");
            String[] s1 = result.split("hs-admin");
            String key = "hs-admin=" + s1[1].trim();
            LOGGER.info("== testJT  ==,ck:{}", key);

            String personalListUrl = "https://console.sivanwallet.com/hs/admin/menu/personalList";
            String[] command2 = {"curl", "--location", "--request", "POST", personalListUrl, "--header", "Content-Type: application/json", "--header", "Cookie:" + key + ";isLogin=ok;"};
            String personalRes = LittleOrangeService.execCurl(command2);
            LOGGER.info("== testJT  ==,personalRes:{}", personalRes);
            String listUrl = "https://console.sivanwallet.com/hs/admin/orderToday/dclist";

            String[] command3 = {"curl", "--location", "--request", "POST", listUrl, "--header", "Content-Type: application/json", "--header", "Cookie:" + key + ";isLogin=ok;isGoogleAuth=ok;",
                    "--data-raw", "{\n" +
                    "  \"fstartTime\": \"2022-07-27 00:00:00\",\n" +
                    "  \"fendTime\": \"2022-07-27 23:59:59\",\n" +
                    "  \"status\": \"\",\n" +
                    "  \"userPhone\": \"\",\n" +
                    "  \"userTrueName\": \"\",\n" +
                    "  \"orderNo\": \"\",\n" +
                    "  \"isDc\": true,\n" +
                    "  \"pageNum\": 1,\n" +
                    "  \"pageSize\": 10\n" +
                    "}"};
            return LittleOrangeService.execCurl(command3);


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @AdminSignIgnore
    @RequestMapping("/ok33")
    @VisitorAccessible
    public String ok33() {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("lang", "en-us");
        headers.put("customer", "3XELTZUO");
        headers.put("auth", "80FDA07DEAA5449A80204D7D3C1674EA-86400");
        headers.put("Referer", "https://client.cooindfodghr.com/cm-dunning-case-list");
        Map<String, String> params = new HashMap<>();
        String body = "{\"query\":{\"pageNo\":1,\"pageSize\":100,\"orderQuery\":{\"userInfo\":{}},\"status\":\"PENDING\",\"filterAllCase\":true}}";
        String s = com.qianli.cashmama.trade.common.utils.HttpUtil.postPageBodyByHttps("https://client.cooindfodghr.com/api/cm_dunning_case/page", headers, body);
        return s;
    }


    @AdminSignIgnore
    @RequestMapping("/paidUpdate/{merchant}")
    @VisitorAccessible
    public String paidUpdate(@PathVariable String merchant) {
        ceAgentAccountDao.getAccountsMerchantCode(merchant).forEach(agentAccount -> {
            chenXingService.updatePaidOrderToCollection(agentAccount);
        });
        return "ok";
    }


}
